package com.xbongbong.crm.service.permission;

import com.xbongbong.crm.help.PublicHelp;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.elasticsearch.util.ElasticSearchConditionUtil;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.parent.abstracts.AbstractBusinessPermissionService;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.permission.pojo.dto.VerifyViewPermissionDTO;
import com.xbongbong.saas.enums.UserTeamEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ClueEnum;
import com.xbongbong.sys.util.UserPermissionUtil;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2022/06/15 16:26
 */
@Service("cluePermissionServiceImpl")
@Slf4j
public class CluePermissionServiceImpl extends AbstractBusinessPermissionService<PaasFormDataEntityExt, PaasFormEntityExt> {


    @Resource
    private PublicHelp publicHelp;

    @Override
    public List<Integer> businessType() {
        return Arrays.asList(XbbRefTypeEnum.CLUE.getCode(), XbbRefTypeEnum.ALL_CLUE.getCode(), XbbRefTypeEnum.CLUE_PUBLIC.getCode());
    }

    @Override
    public boolean checkDelPermission(List<String> userIdIn, PaasFormDataEntityExt paasFormDataEntityExt, Integer businessType) throws XbbException {
        UserTeamEnum byCode = UserTeamEnum.getByCode(businessType);
        BoolQueryBuilder boolQueryBuilder = ElasticSearchConditionUtil.permissionQueryIsMain(paasFormDataEntityExt.getId(), userIdIn, paasFormDataEntityExt.getCorpid(), byCode.getIndexTypeEnum());
        return checkPermissionByTeam(paasFormDataEntityExt.getCorpid(), businessType, boolQueryBuilder);
    }

    @Override
    public Boolean checkPermissionByTeam(String corpid, Integer businessType, BoolQueryBuilder boolQueryBuilder) throws XbbException {
        return checkPermissionByTeam(boolQueryBuilder, IndexTypeEnum.IDX_SAAS_CLUE_TEAM);
    }


    @Override
    public void checkPermission(PaasFormEntityExt formEntity, PaasFormDataEntityExt paasFormDataEntityExt, UserVO loginUser, Integer subBusinessType) throws XbbException {
        VerifyViewPermissionDTO verifyViewPermissionDTO = new VerifyViewPermissionDTO(loginUser, paasFormDataEntityExt);
        //没有团队权限且没有共享抛异常
        boolean hasDataPermission = hasPermission(formEntity, paasFormDataEntityExt, loginUser, subBusinessType)
                || sharePermissionHelp.verifySharePermission4QueryWithRuleEngine(verifyViewPermissionDTO);
        if (!hasDataPermission) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100017);
        }
    }

    @Override
    public boolean hasPermission(PaasFormEntityExt formEntity, PaasFormDataEntityExt paasFormDataEntityExt, UserVO loginUser, Integer subBusinessType) throws XbbException {
        if (UserPermissionUtil.isMaxDataPermission(loginUser, formEntity.getSaasMark(), formEntity.getBusinessType(), formEntity.getDistributorMark(), formEntity.getMenuId(), false)){
            return true;
        }
        if (Objects.equals(paasFormDataEntityExt.getDel(), DelEnum.DELETE.getDel())){
            //回收站线索就不区分了
            return true;
        }
        Long dataId = paasFormDataEntityExt.getId();
        if (Objects.equals(paasFormDataEntityExt.getData().getInteger(ClueEnum.IS_PUBLIC.getAttr()),1)){
            //低权限公海校验
            if (!publicHelp.publicCustomerCheck(loginUser,dataId,paasFormDataEntityExt.getFormId(), formEntity.getBusinessType())) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100017);
            }
            return true;
        }
        String corpid = paasFormDataEntityExt.getCorpid();
        List<String> userIdIn = getUserIdIn(loginUser);
        BoolQueryBuilder boolQueryBuilder = ElasticSearchConditionUtil.permissionQuery(dataId, userIdIn, corpid, IndexTypeEnum.IDX_SAAS_CLUE_TEAM);
        return checkPermissionByTeam(corpid, subBusinessType, boolQueryBuilder);
    }
}
